package com.open.party.cloud.view.home.jiCengDangJian

import android.os.Bundle
import android.view.View
import android.widget.TextView
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import cn.android.x.plugins.webView.WebViewForFilePreviewActivity
import com.open.party.cloud.R
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.PartyDevFileBean
import com.open.party.cloud.model.PartyDevVo
import com.open.party.cloud.view.home.jiCengDangJian.adapter.DyfzDevFileListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.open.party.cloud.viewModel.FileViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperViewListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_app_bg.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DyfzFileListActivity : AppEventBusRecycleViewTitleBaseActivity<PartyDevVo>(), OnSuperViewListener<PartyDevFileBean> {
    private var code = ""
    private var viewModel: BizViewModel? = null
    private var fileViewModel: FileViewModel? = null

    private var adapter: DyfzDevFileListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_app_bg

//    override fun getLineDrawable(): Int {
//        return R.drawable.list_divider_none
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("附件列表")

        code = intent.getStringExtra("code") as String

        viewModel = BizViewModel()
        fileViewModel = FileViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = DyfzDevFileListAdapter(this)
        adapter!!.setOnSuperViewListener(this)
        loadingRecyclerView.adapter = adapter

        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }


    override fun loadData(pageVo: PageVo<PartyDevVo>) {
        val pageReq = PageReq<PartyDevVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val vo = PartyDevVo()
        vo.nodeCode = code
        pageReq.data = vo

        viewModel!!.getDyfzFileListData(pageReq)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<PartyDevFileBean>>) {

        if ("getDyfzFileListData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<PartyDevFileBean>) {

        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: PartyDevFileBean, flag: String?, view: View) {

        when (flag) {
            "operation1Btn" -> {
                if (XUtils.string().isEmpty(itemData.filePath)) {
                    XUtils.toast().show("文件信息不可用")
                    return
                }

                val titleStr = XUtils.string().getNotNullValue(itemData.fileName)
                var url = XUtils.string().getNotNullValue(itemData.filePath)
                if (XUtils.string().isNotEmpty(url)) {
                    WebViewForFilePreviewActivity.start(this, titleStr, ServerConfig.getFilePreviewUrl(url))
                }
            }
            "operation2Btn" -> {
                val oper2tv = view as TextView

                val fileLocPath = XUtils.file().getRootPath(this@DyfzFileListActivity) + "/" + itemData.fileName
                val url = itemData.filePath
//                val url = "http://192.168.0.101:7000/fileCloud/living/10010/images/202002/tiny-613-2020-02-12-01-16-00.jpg"

                fileViewModel!!.downLoadFile(this@DyfzFileListActivity, fileLocPath, url, oper2tv)
            }
        }
    }
}